<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
  <link rel="stylesheet" href="../../styles/common.css" />
  <link rel="stylesheet" href="../../styles/page.css" />
</head>
<body>
  <div class="dashboard-container" id="category-app">
    <div class="container">
      <div
        class="tableBar"
        style="display: inline-block"
      >
        <el-input
                v-model="input"
                placeholder="请输入客户姓名"
                style="width: 250px"
                clearable
                @keyup.enter.native="handleQuery"
        >
          <i
                  slot="prefix"
                  class="el-input__icon el-icon-search"
                  style="cursor: pointer"
                  @click="handleQuery"
          ></i>
        </el-input>
                  <el-button
                    type="primary"
                    @click="addClass()"
                  >
                    + 添加客户
                  </el-button>
      </div>
      <el-table
        :data="tableData"
        stripe
        class="tableBox"
      >
        <el-table-column
          prop="userName"
          label="姓名"
        /></el-table-column>
        <el-table-column
          prop="account"
          label="账号"
        >
        </el-table-column>
        <el-table-column
          prop="telephoneNumber"
          label="手机号"
        >
        </el-table-column>
        <el-table-column
          prop="address"
          label="住址"
        /></el-table-column>
        <el-table-column
          label="操作"
          width="160"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              class="blueBug"
              @click="editHandle(scope.row)"
            >
              修改
            </el-button>
            <el-button
              type="text"
              size="small"
              class="delBut non"
              @click="deleteHandle(scope.row.userId)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pageList"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
    <el-dialog
      :title="classData.title"
      :visible.sync="classData.dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        class="demo-form-inline"
        label-width="100px"
      >
        <el-form-item label="姓名：">
          <el-input
            v-model="classData.userName"
            placeholder="请输入姓名"
            maxlength="14"
          />
        </el-form-item>
        <el-form-item label="性别：">
          <el-input v-model="classData.sex"   placeholder="请输入性别" />
        </el-form-item>
          <el-form-item label="电话：">
              <el-input v-model="classData.telephoneNumber"   placeholder="请输入电话" />
          </el-form-item>
          <el-form-item label="住址：">
              <el-input v-model="classData.address"   placeholder="请输入住址" />
          </el-form-item>
          <el-form-item label="身份证号：">
              <el-input v-model="classData.ssNumber"   placeholder="请输入身份证号" />
          </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          size="medium"
          @click="classData.dialogVisible = false"
        >取 消</el-button>
        <el-button
          type="primary"
          size="medium"
          @click="submitForm()"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
    <!-- 开发环境版本，包含了有帮助的命令行警告 -->
    <script src="../../plugins/vue/vue.js"></script>
    <!-- 引入组件库 -->
    <script src="../../plugins/element-ui/index.js"></script>
    <!-- 引入axios -->
    <script src="../../plugins/axios/axios.min.js"></script>
    <script src="../../js/request.js"></script>
    <script src="../../api/category.js"></script>
    <script>
      new Vue({
        el: '#category-app',
        data() {
          return {
            input: '',
            action: '',
            counts: 0,
            page: 1,
            pageSize: 10,
            tableData: [],
            type :'',
            classData: {

              'title': '',
              'dialogVisible': false,
              // 'categoryId': '',
              // 'name': '',
                'userName': '',
                'sex': '',
                'telephoneNumber' : '',
                'address': '',
                'ssNumber': '',
                'userId': '',
              // sort: ''
            }
          }
        },
        computed: {},
        created() {
          this.init()
        },
        mounted() {
        },
        methods: {
          async init () {
            const params = {
              page: this.page,
              pageSize: this.pageSize,
              name: this.input ? this.input : undefined
            }
            await getCategoryPage(params).then(res => {
              if (String(res.code) === '1') {
                this.tableData = res.data
                this.counts = Number(res.total)
              } else {
                this.$message.error(res.msg || '操作失败')
              }
            }).catch(err => {
              this.$message.error('请求出错了：' + err)
            })
          },
          handleQuery() {
            this.page = 1;
            this.init();
          },
           // 添加
          addClass(st) {
            // if (st == 'class') {

            //   this.type = '1'
            // } else {
            //   this.classData.title = '添加客户'
            //   this.type = '2'
            // }
             this.classData.title = '新增客户'
            this.action = 'add'
              this.classData.userName = ''
              this.classData.sex = ''
              this.classData.telephoneNumber = ''
              this.classData.address = ''
              this.classData.ssNumber = ''
              this.classData.userId =''
            this.classData.dialogVisible = true
          },
          editHandle(dat) {
            this.classData.title = '修改'
            this.action = 'edit'
            this.classData.userName = dat.userName
            this.classData.sex = dat.sex
            this.classData.telephoneNumber = dat.telephoneNumber
              this.classData.address = dat.address
              this.classData.ssNumber = dat.ssNumber
              this.classData.userId = dat.userId;
            this.classData.dialogVisible = true
          },
          // 关闭弹窗
          handleClose(st) {
            this.classData.dialogVisible = false
          },
          //删除
          deleteHandle(id) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
              'confirmButtonText': '确定',
              'cancelButtonText': '取消',
              'type': 'warning'
            }).then(() => {
              deleCategory(id).then(res => {
                if (res.code === 1) {
                  this.$message.success('删除成功！')
                  this.handleQuery()
                } else {
                  this.$message.error(res.msg || '操作失败')
                }
              }).catch(err => {
                this.$message.error('请求出错了：' + err)
              })
            })
          },
          //数据提交
          submitForm(st) {

            function isValidPhoneNumber(phoneNumber) {
              // 这是一个简单的中国手机号码格式验证，你可以根据需要修改
              const reg = /^1[3|4|5|7|8][0-9]{9}$/;
              return reg.test(phoneNumber);
            }

            function isValidSSNumber(ssNumber) {
              // 这是一个简单的中国身份证号码格式验证，你可以根据需要修改
              const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
              return reg.test(ssNumber);
            }


            function isValidSex(sex) {
              // 验证性别，只能是男或女
              return sex === '男' || sex === '女';
            }

              const classData = this.classData

            const nameMap = {
              userName: '姓名',
              sex: '性别',
              telephoneNumber: '电话',
              ssNumber: '身份证号',
              address: '住址'
            }

            console.log(classData.title)

            // 检查classData的每个参数是否为空
            for (let key in classData) {
              if (classData[key] === null || classData[key] === ''  ) {
                // 使用映射对象获取对应的文字
                if (key === 'userId') continue
                console.log(key)
                this.$message.error(nameMap[key] + ' 不能为空！');
                return;
              }
            }

            // 验证电话号码和身份证号
            if (!isValidPhoneNumber(classData.telephoneNumber)) {
              this.$message.error('电话号码格式不正确！');
              return;
            }

            if (!isValidSSNumber(classData.ssNumber)) {
              this.$message.error('身份证号码格式不正确！');
              return;
            }
            // 验证性别
            if (!isValidSex(classData.sex)) {
              this.$message.error('性别只能填男或女！');
              return;
            }
              // const valid = (classData.name === 0 ||classData.name)  && (classData.sort === 0 || classData.sort)
              const  valid = 1
              if (this.action === 'add') {
                if (valid) {
                  const reg = /^\d+$/
                    // reg.test(classData.sort)
                  if (1) {
                    addCategory(this.classData).then(res => {
                      console.log(res)
                      if (res.code === 1) {
                        this.$message.success('客户添加成功！')
                        if (!st) {
                          this.classData.dialogVisible = false
                        } else {
                          this.classData =''
                          // this.classData.sort = ''
                        }
                        this.handleQuery()
                      } else {
                        this.$message.error(res.msg || '操作失败')
                      }
                    }).catch(err => {
                      this.$message.error('请求出错了：' + err)
                    })
                  } else {
                    this.$message.error('排序只能输入数字类型')
                  }
                  
              } else {
                  this.$message.error('请输入分类名称或排序')
              }
            } else if (valid) {
              const reg = /^\d+$/
                  // reg.test(this.classData.sort)
              if (1) {
                  const params = {
                      userName: this.userName,
                      sex: this.sex,
                      telephoneNumber: this.telephoneNumber,
                      ssNumber: this.ssNumber,
                      address: this.address
                  }
                editCategory(classData).then(res => {
                  if (res.code === 1) {
                    this.$message.success('客户修改成功！')
                    this.classData.dialogVisible = false
                    this.handleQuery()
                  } else {
                    this.$message.error(res.msg || '操作失败')
                  }
                }).catch(err => {
                  this.$message.error('请求出错了：' + err)
                })
              } else {
                this.$message.error('排序只能输入数字类型')
              }
            } else {
              this.$message.error('请输入分类名称或排序')
            }
          },
          handleSizeChange (val) {
            this.pageSize = val
            this.init()
          },
          handleCurrentChange (val) {
            this.page = val
            this.init()
          }
        }
      })
    </script>
</body>
</html>